package com.mofro.sql.builder.impl;

import com.mofro.mock.Mock;
import com.mofro.mock.impl.StoreProcedureMock;
import com.mofro.sql.builder.SqlConverter;

/**
 *
 * @author gianu
 */
public class OracleSqlConverter implements SqlConverter {
	
    @Override
	public String getCreateSql(Mock mock) {
		if (mock instanceof StoreProcedureMock) {
			return this.getCreateSql((StoreProcedureMock) mock);
		}
		return null;
	}

	@Override
	public String getDropSql(Mock mock) {
		if (mock instanceof StoreProcedureMock) {
			return this.getDropSql((StoreProcedureMock) mock);
		}
		return null;
	}

	private String getCreateSql(StoreProcedureMock mock) {
        StringBuffer sb = new StringBuffer("create or replace PROCEDURE");
        sb.append(" " + mock.getName());
        sb.append("( ");
        boolean firstParam = true;
        for (String param : mock.getParameters()) {
            if (!firstParam) {
                sb.append(", ");
            }
            sb.append(param);
            firstParam = false;
        }
        sb.append(") AS BEGIN ");

        for (String bodyLine : mock.getBodyLines()) {
            sb.append(bodyLine);
        }

        sb.append(" END " + mock.getName() + ";");

        return sb.toString();
    }

	private String getDropSql(StoreProcedureMock mock) {
        StringBuffer sb = new StringBuffer("drop PROCEDURE");
        sb.append(" " + mock.getName());

        return sb.toString();
    }
}
